<template>
  <div class="main">
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit.prevent="handleLogin"
      style="overflow: hidden;"
    >
      <a-alert
        v-if="isLoginError"
        type="error"
        showIcon
        closable
        style="margin-bottom: 24px;"
        :message="errMsg"
      />
      <a-form-item>
        <a-input
          size="large"
          type="text"
          placeholder="请输入您的用户名"
          v-decorator="['username', { rules: [{ required: true, message: '用户名不能为空' }] }]"
        >
          <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }" />
        </a-input>
      </a-form-item>

      <a-form-item>
        <a-input
          size="large"
          type="password"
          autocomplete="false"
          placeholder="请输入您的登录密码"
          v-decorator="['password', {rules: [{ required: true, message: '密码不能为空' }] }]"
        >
          <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }" />
        </a-input>
      </a-form-item>

      <!-- <a-form-item>
        <a-checkbox v-decorator="['rememberMe']">自动登录</a-checkbox>
        <router-link :to="{ name: 'recover' }" class="forge-password" style="float: right;">忘记密码</router-link>
      </a-form-item>-->

      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="loading"
        >{{loading ? '正在登录...' : '登录'}}</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import md5 from 'js-md5'
import { timeFix, openNotification, removeSession } from '@/utils/util'
import { mapActions } from 'vuex'
import { getUserTrainHistory } from '../../api/archives'

export default {
  data() {
    return {
      redirect: '',
      loading: false,
      isLoginError: false,
      errMsg: '',
      form: this.$form.createForm(this)
    }
  },
  methods: {
    ...mapActions(['Login']),
    handleLogin(e) {
      const _this = getUserTrainHistory
      this.form.validateFields(async (err, data) => {
        if (!err) {
          try {
            data.password = md5(data.password + 'K004')
            // console.log('data', data)
            this.loading = true
            this.Login(data)
              .then(res => this.handleSuccess(res))
              .catch(err => {
                this.handleError(err)
              })
              .finally(() => {
                // state.loginBtn = false
                this.loading = false
              })
            // if (!+res.state) {
            //   _this.handleSuccess(res.data)
            // } else {
            //   _this.handleError(res.message)
            // }
          } catch (e) {}
        }
      })
    },
    async handleSuccess(res) {
      // sessionStorage.auth = JSON.stringify(res)
      // const redirect = this.redirect || '/'
      const redirect = '/'
      this.$router.push({ path: redirect })
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        openNotification('success', '欢迎', `${timeFix()}，欢迎回来`)
      }, 1000)
      this.isLoginError = false
    },
    handleError(err) {
      this.errMsg = err && err.message || '登录失败，未知错误'
      this.isLoginError = true
      // openNotification('error', '错误', err || '请求出现错误，请稍后再试')
    }
  },
  created() {
    this.redirect = this.$route.query.redirect || '/'
    removeSession('haveSeeNotice')
  }
}
</script>

<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
